home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xmbase-grok-1.2 / formfile.c < prev    next >
C/C++ Source or Header  |  1995-06-25  |  14KB  |  432 lines

  1. /*
  2.  * read and write form files.
  3.  *
  4.  *    write_form(form)        write form
  5.  *    read_form(form, path)        read form into empty form struct
  6.  */
  7.  
  8. #include "config.h"
  9. #include <X11/Xos.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <errno.h>
  13. #include <Xm/Xm.h>
  14. #include "grok.h"
  15. #include "form.h"
  16. #include "proto.h"
  17.  
  18. #define STR(s) (s ? s : "")
  19. #define STORE(t,s) do{if(t)free((void*)t);t=(s)&&*(s)?mystrdup(s):0;}while(0)
  20.  
  21. extern int    errno;
  22. extern char    *progname;        /* argv[0] */
  23. extern Widget    toplevel;        /* top-level shell for icon name */
  24.  
  25. extern char    *itemname[NITEMS];    /* name of each item type IT_* */
  26.  
  27.  
  28. /*
  29.  * Write the form and all the items in it. Also create the database named
  30.  * in the form if it doesn't exist.
  31.  * Returns FALSE if the file could not be written.
  32.  */
  33.  
  34. BOOL write_form(
  35.     FORM        *form)        /* form and items to write */
  36. {
  37.     char        *path;        /* file name to write to */
  38.     FILE        *fp;        /* open file */
  39.     DQUERY        *dq;        /* default query pointer */
  40.     ITEM        *item;        /* item pointer */
  41.     int        i;        /* item counter */
  42.     CHART        *chart;        /* chart pointer */
  43.     int        c;        /* chart component counter */
  44.     int        v;        /* chart component value counter */
  45.     char        *p;        /* for printing help text */
  46.  
  47.     path = form->path ? form->path : form->name;
  48.     if (!path || !*path) {
  49.         create_error_popup(toplevel, 0,
  50.             "Form has no name, cannot save to disk");
  51.         return(FALSE);
  52.     }
  53.     if (!form->path)
  54.         path = resolve_tilde(path, "gf");
  55.     if (!(fp = fopen(path, "w"))) {
  56.         create_error_popup(toplevel, errno,
  57.             "Failed to create form file %s", path);
  58.         return(FALSE);
  59.     }
  60.     fprintf(fp, "grok\n");
  61.     fprintf(fp, "name       %s\n",        STR(form->name));
  62.     fprintf(fp, "dbase      %s\n",        STR(form->dbase));
  63.     fprintf(fp, "comment    %s\n",        STR(form->comment));
  64.     fprintf(fp, "cdelim     %s\n",        to_octal(form->cdelim));
  65.     fprintf(fp, "rdonly     %d\n",        form->rdonly);
  66.     fprintf(fp, "proc       %d\n",        form->proc);
  67.     fprintf(fp, "grid       %d %d\n",    form->xg, form->yg);
  68.     fprintf(fp, "size       %d %d\n",    form->xs, form->ys);
  69.     fprintf(fp, "divider    %d\n",        form->ydiv);
  70.     fprintf(fp, "autoq      %d\n",        form->autoquery);
  71.  
  72.     for (p=form->help; p && *p; ) {
  73.         fprintf(fp, "help\t'");
  74.         while (*p && *p != '\n')
  75.             fputc(*p++, fp);
  76.         fputc('\n', fp);
  77.         if (*p) p++;
  78.     }
  79.  
  80.     for (dq=form->query, i=0; i < form->nqueries; i++, dq++) {
  81.         if (!dq->name && !dq->query)
  82.             continue;
  83.         fprintf(fp, "query_s    %d\n",        dq->suspended);
  84.         fprintf(fp, "query_n    %s\n",        STR(dq->name));
  85.         fprintf(fp, "query_q    %s\n",        STR(dq->query));
  86.     }
  87.     for (i=0; i < form->nitems; i++) {
  88.         item = form->items[i];
  89.         fprintf(fp, "\nitem\n");
  90.         fprintf(fp, "type       %s\n",        itemname[item->type]);
  91.         fprintf(fp, "name       %s\n",        STR(item->name));
  92.         fprintf(fp, "pos        %d %d\n",    item->x,  item->y);
  93.         fprintf(fp, "size       %d %d\n",    item->xs, item->ys);
  94.         fprintf(fp, "mid        %d %d\n",    item->xm, item->ym);
  95.         fprintf(fp, "sumwid     %d\n",        item->sumwidth);
  96.         fprintf(fp, "sumcol     %d\n",        item->sumcol);
  97.         fprintf(fp, "column     %d\n",        item->column);
  98.         fprintf(fp, "search     %d\n",        item->search);
  99.         fprintf(fp, "rdonly     %d\n",        item->rdonly);
  100.         fprintf(fp, "nosort     %d\n",        item->nosort);
  101.         fprintf(fp, "defsort    %d\n",        item->defsort);
  102.         fprintf(fp, "timefmt    %d\n",        item->timefmt);
  103.         fprintf(fp, "code       %s\n",        STR(item->flagcode));
  104.         fprintf(fp, "codetxt    %s\n",        STR(item->flagtext));
  105.         fprintf(fp, "label      %s\n",        STR(item->label));
  106.         fprintf(fp, "ljust      %d\n",        item->labeljust);
  107.         fprintf(fp, "lfont      %d\n",        item->labelfont);
  108.         fprintf(fp, "gray       %s\n",        STR(item->gray_if));
  109.         fprintf(fp, "freeze     %s\n",        STR(item->freeze_if));
  110.         fprintf(fp, "invis      %s\n",          STR(item->invisible_if));
  111.         fprintf(fp, "skip       %s\n",        STR(item->skip_if));
  112.         fprintf(fp, "default    %s\n",        STR(item->idefault));
  113.         fprintf(fp, "pattern    %s\n",        STR(item->pattern));
  114.         fprintf(fp, "minlen     %d\n",        item->minlen);
  115.         fprintf(fp, "maxlen     %d\n",        item->maxlen);
  116.         fprintf(fp, "ijust      %d\n",        item->inputjust);
  117.         fprintf(fp, "ifont      %d\n",        item->inputfont);
  118.         fprintf(fp, "p_act      %s\n",        STR(item->pressed));
  119.         fprintf(fp, "a_act      %s\n",        STR(item->added));
  120.         fprintf(fp, "q_dbase    %s\n",        STR(item->database));
  121.         fprintf(fp, "query      %s\n",        STR(item->query));
  122.         fprintf(fp, "q_summ     %d\n",        item->qsummary);
  123.         fprintf(fp, "q_first    %d\n",        item->qfirst);
  124.         fprintf(fp, "q_last     %d\n",        item->qlast);
  125.         fprintf(fp, "q_last     %d\n",        item->qlast);
  126.  
  127.         fprintf(fp, "ch_xrange  %g %g\n",    item->ch_xmin,
  128.                             item->ch_xmax);
  129.         fprintf(fp, "ch_yrange  %g %g\n",    item->ch_ymin,
  130.                             item->ch_ymax);
  131.         fprintf(fp, "ch_auto    %d %d\n",    item->ch_xauto,
  132.                             item->ch_yauto);
  133.         fprintf(fp, "ch_grid    %g %g\n",    item->ch_xgrid,
  134.                             item->ch_ygrid);
  135.         fprintf(fp, "ch_snap    %g %g\n",    item->ch_xsnap,
  136.                             item->ch_ysnap);
  137.         fprintf(fp, "ch_label   %g %g\n",    item->ch_xlabel,
  138.                             item->ch_ylabel);
  139.         fprintf(fp, "ch_xexpr   %s\n",        STR(item->ch_xexpr));
  140.         fprintf(fp, "ch_yexpr   %s\n",        STR(item->ch_yexpr));
  141.         fprintf(fp, "ch_ncomp   %d\n",        item->ch_ncomp);
  142.  
  143.         for (c=0; c < item->ch_ncomp; c++) {
  144.             chart = &item->ch_comp[c];
  145.             fprintf(fp, "chart\n");
  146.             fprintf(fp, "_ch_type   %d\n",    chart->line);
  147.             fprintf(fp, "_ch_fat    %d %d\n",    chart->xfat,
  148.                             chart->yfat);
  149.             fprintf(fp, "_ch_excl   %s\n",    STR(chart->excl_if));
  150.             fprintf(fp, "_ch_color  %s\n",    STR(chart->color));
  151.             fprintf(fp, "_ch_label  %s\n",    STR(chart->label));
  152.             for (v=0; v < 4; v++) {
  153.             struct value *val = &chart->value[v];
  154.             fprintf(fp, "_ch_mode%d  %d\n",    v, val->mode);
  155.             fprintf(fp, "_ch_expr%d  %s\n",    v, STR(val->expr));
  156.             fprintf(fp, "_ch_field%d %d\n",    v, val->field);
  157.             fprintf(fp, "_ch_mul%d   %g\n",    v, val->mul);
  158.             fprintf(fp, "_ch_add%d   %g\n",    v, val->add);
  159.             }
  160.         }
  161.     }
  162.     fclose(fp);
  163.     remake_dbase_pulldown();
  164.  
  165.     path = resolve_tilde(form->dbase, "db");
  166.     if (access(path, F_OK) && errno == ENOENT) {
  167.         if (!(fp = fopen(path, "w"))) {
  168.             create_error_popup(toplevel, errno,
  169. "The form was created successfully, but the\n\
  170. database file %s cannot be created.\n\
  171. No cards can be entered into the new Form.\n\nProblem: ", path);
  172.             return(FALSE);
  173.         }
  174.         fclose(fp);
  175.     }
  176.     if (access(path, R_OK)) {
  177.         create_error_popup(toplevel, errno,
  178. "The form was created successfully, but the\n\
  179. database file %s exists but is not readable.\n\
  180. No cards can be entered into the new Form.\n\nProblem: ", path);
  181.         return(FALSE);
  182.     }
  183.     return(TRUE);
  184. }
  185.  
  186.  
  187. /*
  188.  * Read the form and all the items in it from a file.
  189.  * Returns FALSE and leave *form untouched if the file could not be read.
  190.  */
  191.  
  192. BOOL read_form(
  193.     FORM            *form,        /* form and items to write */
  194.     char            *path)        /* file to read list from */
  195. {
  196.     FILE            *fp;        /* open file */
  197.     char            line[1024];    /* line buffer */
  198.     char            *p, *key;    /* next char in line buf */
  199.     DQUERY            *dq = 0;    /* default query pointer */
  200.     ITEM            *item = 0;    /* item pointer, 0=form hdr */
  201.     int            i;        /* item counter */
  202.     CHART            *chart;        /* next chart component */
  203.     CHART            nullchart;    /* if chart keyword missing */
  204.  
  205.     path = resolve_tilde(path, "gf");
  206.     if (!(fp = fopen(path, "r"))) {
  207.         create_error_popup(toplevel, errno,
  208.             "Failed to open form file %s", path);
  209.         return(FALSE);
  210.     }
  211.     form_delete(form);
  212.     if (form->path)
  213.         free(form->path);
  214.     form->path = mystrdup(path);
  215.     for (;;) {
  216.         if (!fgets(line, 1023, fp))
  217.             break;
  218.         if ((i = strlen(line)) && line[i-1] == '\n')
  219.             line[i-1] = 0;
  220.         for (p=line; *p == ' ' || *p == '\t'; p++);
  221.         if (*p == '#' || !*p)
  222.             continue;
  223.         for (key=p; *p && *p != ' ' && *p != '\t'; p++);
  224.         if (*p)
  225.             *p++ = 0;
  226.         else
  227.             *++p = 0;
  228.         for (; *p == ' ' || *p == '\t'; p++);
  229.  
  230.         if (!strcmp(key, "item")) {
  231.             if (!item_create(form, form->nitems)) {
  232.                 form_delete(form);
  233.                 return(FALSE);
  234.             }
  235.             chart = &nullchart;
  236.             item  = form->items[form->nitems-1];
  237.             item->selected = FALSE;
  238.             item->ch_curr  = 0;
  239.                                 /* header */
  240.         } else if (!item) {
  241.             if (!strcmp(key, "grok"))
  242.                     ;
  243.             else if (!strcmp(key, "name"))
  244.                     STORE(form->name, p);
  245.             else if (!strcmp(key, "dbase"))
  246.                     STORE(form->dbase, p);
  247.             else if (!strcmp(key, "comment"))
  248.                     STORE(form->comment, p);
  249.             else if (!strcmp(key, "rdonly"))
  250.                     {sscanf(p, "%d", &i); form->rdonly=i;}
  251.             else if (!strcmp(key, "proc"))
  252.                     {sscanf(p, "%d", &i); form->proc=i;}
  253.             else if (!strcmp(key, "cdelim"))
  254.                     form->cdelim = to_ascii(p, ':');
  255.             else if (!strcmp(key, "grid"))
  256.                     sscanf(p, "%d %d",&form->xg,&form->yg);
  257.             else if (!strcmp(key, "size"))
  258.                     sscanf(p, "%d %d",&form->xs,&form->ys);
  259.             else if (!strcmp(key, "divider"))
  260.                     sscanf(p, "%d", &form->ydiv);
  261.             else if (!strcmp(key, "autoq"))
  262.                     sscanf(p, "%d", &form->autoquery);
  263.             else if (!strcmp(key, "query_s")) {
  264.                 if (dq = add_dquery(form))
  265.                     dq->suspended = *p != '0';
  266.             } else if (!strcmp(key, "query_n")) {
  267.                 if (dq)
  268.                     STORE(dq->name, p);
  269.             } else if (!strcmp(key, "query_q")) {
  270.                 if (dq)
  271.                     STORE(dq->query, p);
  272.             } else if (!strcmp(key, "help") && *p++ == '\'') {
  273.                 if (form->help) {
  274.                     if (form->help = realloc(form->help,
  275.                                strlen(form->help) +
  276.                                strlen(p) + 2))
  277.                         strcat(form->help, p);
  278.                 } else {
  279.                     if (form->help = malloc(strlen(p) + 2))
  280.                         strcpy(form->help, p);
  281.                 }
  282.                 if (form->help)
  283.                     strcat(form->help, "\n");
  284.             } else
  285.                 fprintf(stderr,
  286.                     "%s: %s: ignored headers keyword %s\n",
  287.                             progname, path, key);
  288.  
  289.         } else {                    /* item */
  290.             if (!strcmp(key, "type")) {
  291.                 for (i=0; i < NITEMS; i++)
  292.                     if (!strcmp(itemname[i], p))
  293.                         break;
  294.                 if (i == NITEMS)
  295.                     fprintf(stderr,
  296.                         "%s: %s: bad item type %s\n",
  297.                         progname, path, p);
  298.                 item->type = i;
  299.             }
  300.             else if (!strcmp(key, "name"))
  301.                     STORE(item->name, p);
  302.             else if (!strcmp(key, "pos"))
  303.                     sscanf(p, "%d %d",&item->x, &item->y);
  304.             else if (!strcmp(key, "size"))
  305.                     sscanf(p,"%d %d", &item->xs,&item->ys);
  306.             else if (!strcmp(key, "mid"))
  307.                     sscanf(p,"%d %d", &item->xm,&item->ym);
  308.             else if (!strcmp(key, "sumwid"))
  309.                     item->sumwidth = atoi(p);
  310.             else if (!strcmp(key, "sumcol"))
  311.                     item->sumcol = atoi(p);
  312.             else if (!strcmp(key, "column"))
  313.                     item->column = atoi(p);
  314.             else if (!strcmp(key, "search"))
  315.                     item->search = atoi(p) ? TRUE : FALSE;
  316.             else if (!strcmp(key, "rdonly"))
  317.                     item->rdonly = atoi(p) ? TRUE : FALSE;
  318.             else if (!strcmp(key, "nosort"))
  319.                     item->nosort = atoi(p) ? TRUE : FALSE;
  320.             else if (!strcmp(key, "defsort"))
  321.                     item->defsort = atoi(p) ? TRUE : FALSE;
  322.             else if (!strcmp(key, "timefmt"))
  323.                     item->timefmt = atoi(p);
  324.             else if (!strcmp(key, "code"))
  325.                     STORE(item->flagcode, p);
  326.             else if (!strcmp(key, "codetxt"))
  327.                     STORE(item->flagtext, p);
  328.             else if (!strcmp(key, "label"))
  329.                     STORE(item->label, p);
  330.             else if (!strcmp(key, "ljust"))
  331.                     item->labeljust = atoi(p);
  332.             else if (!strcmp(key, "lfont"))
  333.                     item->labelfont = atoi(p);
  334.             else if (!strcmp(key, "gray"))
  335.                     STORE(item->gray_if, p);
  336.             else if (!strcmp(key, "freeze"))
  337.                     STORE(item->freeze_if, p);
  338.             else if (!strcmp(key, "invis"))
  339.                     STORE(item->invisible_if, p);
  340.             else if (!strcmp(key, "skip"))
  341.                     STORE(item->skip_if, p);
  342.             else if (!strcmp(key, "default"))
  343.                     STORE(item->idefault, p);
  344.             else if (!strcmp(key, "pattern"))
  345.                     STORE(item->pattern, p);
  346.             else if (!strcmp(key, "minlen"))
  347.                     item->minlen = atoi(p);
  348.             else if (!strcmp(key, "maxlen"))
  349.                     item->maxlen = atoi(p);
  350.             else if (!strcmp(key, "ijust"))
  351.                     item->inputjust = atoi(p);
  352.             else if (!strcmp(key, "ifont"))
  353.                     item->inputfont = atoi(p);
  354.             else if (!strcmp(key, "p_act"))
  355.                     STORE(item->pressed, p);
  356.             else if (!strcmp(key, "a_act"))
  357.                     STORE(item->added, p);
  358.             else if (!strcmp(key, "q_dbase"))
  359.                     STORE(item->database, p);
  360.             else if (!strcmp(key, "query"))
  361.                     STORE(item->query, p);
  362.             else if (!strcmp(key, "q_summ"))
  363.                     item->qsummary = atoi(p);
  364.             else if (!strcmp(key, "q_first"))
  365.                     item->qfirst = atoi(p);
  366.             else if (!strcmp(key, "q_last"))
  367.                     item->qlast = atoi(p);
  368.  
  369.             else if (!strcmp(key, "ch_xrange"))
  370.                     sscanf(p, "%g %g", &item->ch_xmin,
  371.                                &item->ch_xmax);
  372.             else if (!strcmp(key, "ch_yrange"))
  373.                     sscanf(p, "%g %g", &item->ch_ymin,
  374.                                &item->ch_ymax);
  375.             else if (!strcmp(key, "ch_auto"))
  376.                     sscanf(p, "%d %d", &item->ch_xauto,
  377.                                &item->ch_yauto);
  378.             else if (!strcmp(key, "ch_grid"))
  379.                     sscanf(p, "%g %g", &item->ch_xgrid,
  380.                                &item->ch_ygrid);
  381.             else if (!strcmp(key, "ch_snap"))
  382.                     sscanf(p, "%g %g", &item->ch_xsnap,
  383.                                &item->ch_ysnap);
  384.             else if (!strcmp(key, "ch_label"))
  385.                     sscanf(p, "%g %g", &item->ch_xlabel,
  386.                                &item->ch_ylabel);
  387.             else if (!strcmp(key, "ch_xexpr"))
  388.                     STORE(item->ch_xexpr, p);
  389.             else if (!strcmp(key, "ch_yexpr"))
  390.                     STORE(item->ch_yexpr, p);
  391.             else if (!strcmp(key, "ch_ncomp")) {
  392.                     if (item->ch_ncomp = atoi(p))
  393.                         item->ch_comp =
  394.                             calloc(item->ch_ncomp,
  395.                                 sizeof(CHART));
  396.             }
  397.             else if (!strcmp(key, "chart"))
  398.                     chart = chart == &nullchart ?
  399.                         item->ch_comp : chart+1;
  400.             else if (!strcmp(key, "_ch_type"))
  401.                     chart->line = atoi(p);
  402.             else if (!strcmp(key, "_ch_fat"))
  403.                     sscanf(p, "%d %d", &chart->xfat,
  404.                                &chart->yfat);
  405.             else if (!strcmp(key, "_ch_excl"))
  406.                     STORE(chart->excl_if, p);
  407.             else if (!strcmp(key, "_ch_color"))
  408.                     STORE(chart->color, p);
  409.             else if (!strcmp(key, "_ch_label"))
  410.                     STORE(chart->label, p);
  411.             else if (!strncmp(key, "_ch_mode", 8))
  412.                     chart->value[key[8]-'0'].mode =atoi(p);
  413.             else if (!strncmp(key, "_ch_field", 9))
  414.                     chart->value[key[9]-'0'].field=atoi(p);
  415.             else if (!strncmp(key, "_ch_mul", 7))
  416.                     chart->value[key[7]-'0'].mul = atof(p);
  417.             else if (!strncmp(key, "_ch_add", 7))
  418.                     chart->value[key[7]-'0'].add = atof(p);
  419.             else if (!strncmp(key, "_ch_expr", 8))
  420.                     STORE(chart->value[key[8]-'0'].expr,p);
  421. #if 0
  422.             else
  423.                 fprintf(stderr,
  424.                     "%s: %s: ignored item keyword %s\n",
  425.                             progname, path, key);
  426. #endif
  427.         }
  428.     }
  429.     fclose(fp);
  430.     return(TRUE);
  431. }
  432.